__DATA__
package PI::Configuration::MainTest;

use base 'Test::Unit::TestCase';

use PI::Configuration::Main;
use Test::MockObject::Extends;

use File::Spec;
use PI::System::Path;

my $file;
my $lock;
my $default;

sub new {
    my $self = shift()->SUPER::new(@_);
    return $self;
}

sub set_up {
    $file = PI::Configuration::MainForTest->file;
    $lock = $file . '.lock';
}

sub tear_down {
    unlink $file, $file."-$$" if $file;
    unlink $lock if $lock;
}

sub test_PI_Configuration_Main_isa {
    my $self = shift;
    my $obj = PI::Configuration::MainForTest->instance;
    $self->assert_not_null($obj);
    $self->assert($obj->isa('PI::Configuration::MainForTest'));
}

sub test_PI_Configuration_Main_singleton {
    my $self = shift;
    my $obj1 = PI::Configuration::Main->instance;
    my $obj2 = PI::Configuration::Main->instance;
    $self->assert_not_null($obj1);
    $self->assert_not_null($obj2);
    $self->assert($obj1->isa('PI::Configuration::Main'));
    $self->assert($obj2->isa('PI::Configuration::Main'));
    $self->assert_equals($obj1, $obj2);
}

sub test_PI_Configuration_Main_accessor_file {
    my $self = shift;
    my $obj = PI::Configuration::MainForTest->instance;
    $self->assert_equals($file, $obj->file);
}

sub test_PI_Configuration_Main_accessor_data {
    my $self = shift;
    my $obj = PI::Configuration::MainForTest->instance;
    my $default_area = $obj->data->{default_area};
    $self->assert_not_null($default_area);
    $obj->data->{default_area} = 'default_area';
    $self->assert_equals('default_area', $obj->data->{default_area});
    $obj->data->{default_area} = $default_area;
}

sub test_PI_Configuration_Main_save {
    my $self = shift;
    my $obj = PI::Configuration::MainForTest->instance;
    my $data = $obj->data;
    unlink $file;
    $self->assert(not -f $file);
    $obj->save;
    $self->assert(-f $file);
    $obj->load;
    $self->assert_not_equals($data, $obj->data);
    $self->assert_deep_equals($data, $obj->data);
}

1;


package PI::Configuration::MainForTest;

use base 'PI::Configuration::Main';

use File::Spec;
use PI::System::Path;

sub file {
    File::Spec->catfile(PI::System::Path->tmpdir, sprintf('config-%s-%s.yml', $^T, $$));
}

1;
